package cn.cttic.order.service.impl;

import cn.cttic.common.core.utils.MapstructUtils;
import cn.cttic.common.core.utils.StringUtils;
import cn.cttic.common.mybatis.core.page.TableDataInfo;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.order.api.domain.bo.BaseFilingsQueryBo;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import cn.cttic.order.api.domain.bo.BaseFilingsBo;
import cn.cttic.order.api.domain.vo.BaseFilingsVo;
import cn.cttic.order.api.domain.BaseFilings;
import cn.cttic.order.mapper.BaseFilingsMapper;
import cn.cttic.order.service.IBaseFilingsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 备案信息Service业务层处理
 *
 * @author Times.Studio
 * @date 2024-09-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BaseFilingsServiceImpl implements IBaseFilingsService {

    private final BaseFilingsMapper baseMapper;

    /**
     * 查询备案信息
     *
     * @param id 主键
     * @return 备案信息
     */
    @Override
    public BaseFilingsVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询备案信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 备案信息分页列表
     */
    @Override
    public TableDataInfo<BaseFilingsVo> queryPageList(BaseFilingsQueryBo bo, PageQuery pageQuery) {
        QueryWrapper<BaseFilings> qw = buildQueryWrapper(bo);
        Page<BaseFilingsVo> result = baseMapper.getFilingPageList(pageQuery.build(), qw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的备案信息列表
     *
     * @param bo 查询条件
     * @return 备案信息列表
     */
    @Override
    public List<BaseFilingsVo> queryList(BaseFilingsQueryBo bo) {
        QueryWrapper<BaseFilings> qw = buildQueryWrapper(bo);
        return baseMapper.getFilingList(qw);
    }

    private QueryWrapper<BaseFilings> buildQueryWrapper(BaseFilingsQueryBo bo) {
        log.info("备案信息 查询参数 {}", bo);
//        log.info(">>1>> {}", bo.getLastOnlineTime());
//        log.info(">>2>> {}", ObjUtil.isNotEmpty(bo.getLastOnlineTime()));
        Map<String, Object> params = bo.getParams();
        QueryWrapper<BaseFilings> lqw = Wrappers.query();
        lqw.like(StringUtils.isNotBlank(bo.getCustomerName()), "saleOrder.last_name", bo.getCustomerName());
        lqw.like(StringUtils.isNotBlank(bo.getCustomerPhone()), "saleOrder.phone", bo.getCustomerPhone());
        lqw.like(StringUtils.isNotBlank(bo.getCarNo()), "installOrder.car_no", bo.getCarNo());
        lqw.like(StringUtils.isNotBlank(bo.getImei()), "installOrder.imei", bo.getImei());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), "product.product_name", bo.getProductName());
        lqw.like(StringUtils.isNotBlank(bo.getSaleChannelName()), "salesManCompany.company_name", bo.getSaleChannelName());
        lqw.like(StringUtils.isNotBlank(bo.getInstallChannelName()), "installManCompany.company_name", bo.getInstallChannelName());
        lqw.like(StringUtils.isNotBlank(bo.getSaleChannelTypeName()), "saleChannelDic.dict_label", bo.getSaleChannelTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getInstallChannelTypeName()), "installChannelDic.dict_label", bo.getInstallChannelTypeName());
        lqw.like(StringUtils.isNotBlank(bo.getInstallManName()), "installMan.real_name", bo.getInstallManName());
        lqw.eq(ObjUtil.isNotEmpty(bo.getVerifyStatus()), "installOrder.verify_status", bo.getVerifyStatus());
        lqw.ge(ObjUtil.isNotEmpty(bo.getLastOnlineTimeBegin()), "installOrder.last_online_time", bo.getLastOnlineTimeBegin());
        lqw.le(ObjUtil.isNotEmpty(bo.getLastOnlineTimeEnd()), "installOrder.last_online_time", bo.getLastOnlineTimeEnd());
        lqw.ge(ObjUtil.isNotEmpty(bo.getOnlineTimeBegin()), "installOrder.online_time", bo.getOnlineTimeBegin());
        lqw.le(ObjUtil.isNotEmpty(bo.getOnlineTimeEnd()), "installOrder.online_time", bo.getOnlineTimeEnd());
        lqw.ge(ObjUtil.isNotEmpty(bo.getActiveTimeBegin()), "installOrder.active_time", bo.getActiveTimeBegin());
        lqw.le(ObjUtil.isNotEmpty(bo.getActiveTimeEnd()), "installOrder.active_time", bo.getActiveTimeEnd());
        lqw.ge(ObjUtil.isNotEmpty(bo.getVerifyTimeBegin()), "installOrder.verify_time", bo.getVerifyTimeBegin());
        lqw.le(ObjUtil.isNotEmpty(bo.getVerifyTimeEnd()), "installOrder.verify_time", bo.getVerifyTimeEnd());
//        if (ObjUtil.isNotEmpty(bo.getLastOnlineTime())) {
//            log.info(">>3>> {}", ObjUtil.isNotEmpty(bo.getLastOnlineTime()));
//            lqw.eq(ObjUtil.isNotEmpty(bo.getLastOnlineTime().getDate()), "DATE(installOrder.last_online_time)", bo.getLastOnlineTime().getDate());
//        }
//        if (ObjUtil.isNotEmpty(bo.getOnlineTime())) {
//            log.info(">>4>> {}", ObjUtil.isNotEmpty(bo.getOnlineTime()));
//            lqw.eq(ObjUtil.isNotEmpty(bo.getOnlineTime().getDate()), "DATE(installOrder.online_time)", bo.getOnlineTime().getDate());
//        }
//        if (ObjUtil.isNotEmpty(bo.getActiveTime())) {
//            log.info(">>5>> {}", ObjUtil.isNotEmpty(bo.getActiveTime()));
//            lqw.eq(ObjUtil.isNotEmpty(bo.getActiveTime().getDate()), "DATE(installOrder.active_time)", bo.getActiveTime().getDate());
//        }
//        if (ObjUtil.isNotEmpty(bo.getVerifyTime())) {
//            log.info(">>6>> {}", ObjUtil.isNotEmpty(bo.getVerifyTime()));
//            lqw.eq(ObjUtil.isNotEmpty(bo.getVerifyTime().getDate()), "DATE(installOrder.verify_time)", bo.getVerifyTime().getDate());
//        }
        return lqw;
    }

    /**
     * 新增备案信息
     *
     * @param bo 备案信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseFilingsBo bo) {
        BaseFilings add = MapstructUtils.convert(bo, BaseFilings.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改备案信息
     *
     * @param bo 备案信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseFilingsBo bo) {
        BaseFilings update = MapstructUtils.convert(bo, BaseFilings.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseFilings entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除备案信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
